package com.yyge.controller.admin.orders;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yyge.common.controller.BaseController;
import com.yyge.common.enums.AckCode;
import com.yyge.common.util.WaimaiThreadLocal;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.common.vo.resp.R;
import com.yyge.controller.admin.login.resp.AdminLoginResp;
import com.yyge.controller.admin.mapstruct.OrdersConverter;
import com.yyge.controller.admin.orders.req.OrdersReq;
import com.yyge.controller.admin.orders.resp.OrdersResp;
import com.yyge.controller.admin.orders.resp.OrdersStatisticResp;
import com.yyge.entity.Orders;
import com.yyge.service.OrdersService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

@RestController
@RequestMapping("/api/orders")
public class OrdersController extends BaseController<Orders,Object> {

    public OrdersController() {
        this.converter=OrdersConverter.class;
    }
    // 注入业务逻辑层
    private OrdersService service;

    @Autowired/*自动装配*/
    public void setService(OrdersService ordersService){
        this.service=ordersService;
    }

    @GetMapping("data")/* GET(SELECT): 查询 */
    public R<PageResp<OrdersResp>> get(@Valid OrdersReq req) throws Exception  {
        AdminLoginResp current = WaimaiThreadLocal.getCurrent();
        if(current.getType()==2){
            req.setMerchantId(current.getId());
        }
        PageInfo<OrdersResp> pageInfo=this.service.search(req);
        PageResp<OrdersResp> resp=new PageResp<>();
        resp.setRows(pageInfo.getList());
        resp.setTotal(pageInfo.getTotal());
        return R.okHasData(resp);
    }

    @ApiOperation(value = "订单统计数据")
    @GetMapping(value = "/statistic")
    public R<OrdersStatisticResp> ordersStatistic(){
        OrdersStatisticResp resp= this.service.ordersStatistic();
        return R.okHasData(resp);
    }

    @PostMapping/* POST(CREATE): 新增&登陆 */
    public R post(@RequestBody Orders orders){
        Boolean i=service.save(orders);
        return i?R.ok("新增成功！"):R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR);
    }

    @GetMapping("/{id}")/* POST(CREATE): 根据ID主键进行查询 */
    public R getById(@PathVariable("id") String id){
        Orders byId = service.getById(id);
        return R.okHasData(byId);
        //return byId?R.ok("新增成功！"):R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR);
    }



    @PutMapping/* POST(CREATE): 更新 */
    public R out(@RequestBody Orders orders){
        Boolean i=service.updateById(orders);
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody Orders orders){
        return null;
    }

    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@PathVariable("id") Integer[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_DELETE_ERROR);
    }
}

